package CliGui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/*
两个面板
上面显示聊天记录
下面为输入框
 */

/*

 */
public class ChatGui extends JFrame {
    private String inputmsg;

    //等于1则是有消息，外面发送线程把消息取出来然后置0
    private int flag = 0;

    //聊天面板
    JTextArea chatArea = new JTextArea();
    //创建一个可滚动的视图区域
    JScrollPane rollChat = new JScrollPane(chatArea);

    public ChatGui(){
        init();
    }

    public void init(){
        setTitle("聊天软件");
        setBounds(300,300,600,500);

        //主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        //聊天，输入，按钮面板
        JPanel chatPanel = new JPanel(new BorderLayout());
        JPanel buttonPanel = new JPanel(new GridLayout(1,2));
        JPanel inputPanel = new JPanel(new GridLayout());


        chatArea.setEditable(false);        //不可编辑
        chatPanel.add(rollChat,BorderLayout.CENTER);


        //输入框面板
        JTextField text = new JTextField();
        inputPanel.add(text);
        inputPanel.setPreferredSize(new Dimension(600,70));

        //按钮面板
        JButton confirm = new JButton("发送");
        JButton exit = new JButton("清空");
        //设置首选大小
        confirm.setPreferredSize(new Dimension(100,60));
        exit.setPreferredSize(new Dimension(100,60));
        buttonPanel.add(exit);
        buttonPanel.add(confirm);


        //下方面板（输入框+按钮）
        JPanel southPanel = new JPanel(new BorderLayout());
        southPanel.add(inputPanel,BorderLayout.CENTER);
        southPanel.add(buttonPanel,BorderLayout.SOUTH);


        mainPanel.add(chatPanel,BorderLayout.CENTER);
        mainPanel.add(southPanel,BorderLayout.SOUTH);

        //改字体
        chatArea.setFont(new Font("宋体",Font.PLAIN,23));
        text.setFont(new Font("宋体",Font.PLAIN,23));

        add(mainPanel);
        setVisible(true);

        //事件监听
        //回车，清空，发送
        text.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if(key == KeyEvent.VK_ENTER){
                    inputmsg = text.getText();
                    text.setText("");
                    chatArea.append("本地："+inputmsg+"\n");
                    flag = 1;
                }
            }
        });

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                inputmsg = text.getText();
                text.setText("");
                chatArea.append("本地："+inputmsg+"\n");
                flag = 1;
            }
        });

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                text.setText("");
            }
        });
    }

    //接收线程
    public void getNewMsg(String msg){
        chatArea.append(msg+"\n");
    }

    //只在发送线程中用
    public int getFlag(){
        return this.flag;
    }

    public void setFlag(int f){
        this.flag = f;
    }

    public String getInputmsg(){
        return this.inputmsg;
    }

}
